<script>
	import { dialogs } from "@/components/dialog/dialogUtil"
	import store from './store'
	import { userApi } from '@/http/userApi.js'
	import { USER_TOKEN } from '@/constants/local-storage-key-const'
	import { manner } from "@/util/manner.js"
	export default {
		onLaunch: async function() {
			console.log('App onLaunch');
			let that = this;
			uni['dialog'] = dialogs
			// await that.$nextTick()
			uni.onPushMessage(res => {
				console.log('监听推送消息', res)
				const { type, data } = res
				if (type == 'click') {
					// console.log('"click"-从系统推送服务点击消息启动应用事件2；', res)
					// if (!data?.payload?.url) {
					// 	uni.reLaunch({
					// 		url: '/pages/index/index',
					// 	});
					// } else {
					// 	setTimeout(() => {
					// 		uni.navigateTo({
					// 			url: data.payload.url,
					// 		});
					//   }, 1000);
					// }
				}
				if (type == 'receive') {
					const whiteList = ['/pagesMain/mplogin/mplogin', '/pagesMain/Initial-info/Initial-info']
					const currentPage = getCurrentPages().pop().route
					if (whiteList.includes(currentPage)) {
						return
					}
					res.data.payload = JSON.parse(JSON.parse(res.data.payload))
					let actionCode = res.data.payload.actionCode
					if (actionCode == 'user_visit' || actionCode == 'user_concern' || actionCode == 'user_favor' || actionCode == 'blog_comment' || actionCode == 'blog_collect') {
						console.log("添加小红点");
						uni.setStorageSync('notice', true);
						store.commit('setNotice', true);
					}

					if (actionCode == 'couple_recommend') {
						// #ifdef APP
						uni.dialog.alert({
							title: res.data.title,
							content: res.data.content,
							image: res.data.payload.avatar,
							actionCode: actionCode,
							age: res.data.payload.age,
							gender: res.data.payload.gender,
							ipProvince: res.data.payload.ipProvince,
							gameName: res.data.payload.gameName,
							confirmText: actionCode == 'couple_recommend' ? '去聊天' : '去查看',
							confirm: () => {
								if (actionCode == 'couple_recommend') {
									const item = {
										id: res.data.payload.uid,
										name: res.data.payload.nickname,
										avatar: res.data.payload.avatar,
										conversationID: 'C2C' + res.data.payload.uid,
										type: 'C2C'
									}
									uni.navigateTo({
										url: '/message/im/im?to=' + encodeURIComponent(JSON.stringify(item))
									})
								}
							}
						});
						// #endif
					}
				}
			});
			const token = uni.getStorageSync(USER_TOKEN)
			that.setWxHeader()
			if (token) {
				that.setUserInfo()
			} else {
				uni.redirectTo({
					url: '/pagesMain/mplogin/mplogin',
					success() {
						// #ifdef APP
						setTimeout(() => {
							plus.navigator.closeSplashscreen();
						}, 300)
						// #endif
					}
				});
			}
		},
		onShow: function() {
			console.log('App Show')
			// uni.dialog.alert({
			// 	title: '搭子推荐',
			// 	content: 'Pansers',
			// 	image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/appUpload/20250402162449-6505c571-1bef-473d-a0a2-484810e032fa.jpg',
			// 	actionCode: 'couple_recommend',
			// 	confirmText: '去聊天',
			// 	confirm: () => {
			// 	}
			// });
			const token = uni.getStorageSync(USER_TOKEN)
			if (token) {
				console.log('onlineStatus: ', store.state.user.onlineStatus);
				if (store.state.user.onlineStatus == 1) {
					this.ChangeOnlineStatus('online')
				} else if (store.state.user.onlineStatus == 3) {
					this.ChangeOnlineStatus('invisible')
				} else {
					this.ChangeOnlineStatus('online')
				}
			}
			let totalUnreadCount = store.state.UnReadCount
			if(totalUnreadCount != '0') {
				uni.setTabBarBadge({
					index: 3,
					text: totalUnreadCount,
				});
			} else {
				uni.removeTabBarBadge({
					index: 3,
				});
			}
			const firstTips = uni.getStorageSync('firstTips');
			if(!firstTips) {
				uni.setStorageSync('firstTips', 'ShowTips');
			}
			// let isRecomend = uni.getStorageSync('isRecomend')
			// if(isRecomend){
			// 	let rec = isRecomend == '1'?true:false
			// 	store.commit('setHotRecommend',rec)
			// }
		},
		onHide: function() {
			console.log('App Hide')
			this.ChangeOnlineStatus('offline')
		},
		methods: {
			setWxHeader() {
				let statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
				// #ifdef MP 
				let menuButtonHeight = uni.getMenuButtonBoundingClientRect().height;
				let titleBarWidth = uni.getSystemInfoSync().windowWidth - uni.getMenuButtonBoundingClientRect().width - (
					uni.getSystemInfoSync().windowWidth - uni.getMenuButtonBoundingClientRect().right) * 2;
				let titleBarHeight = (uni.getMenuButtonBoundingClientRect().top - statusBarHeight) * 2 + menuButtonHeight;
				store.commit('setWxHeader', {
					'statusBarHeight': statusBarHeight,
					'menuButtonHeight': menuButtonHeight,
					'titleBarWidth': titleBarWidth,
					'titleBarHeight': titleBarHeight
				})
				// #endif
				// #ifdef APP
				store.commit('setStatusBarHeight', statusBarHeight)
				// #endif
			},
			async setUserInfo() {
				try {
					let params = {
						dressedVoMap: 1
					}
					const usr = await userApi.getUserInfo(params)
					console.log('userinfo',usr)
					store.commit('setLogin', usr)
					this.imLogin(usr.uid, usr.imSign)
					// #ifdef APP
					setTimeout(() => {
						plus.navigator.closeSplashscreen();
					}, 300)
					// #endif
				} catch (e) {
					// #ifdef APP
					setTimeout(() => {
						plus.navigator.closeSplashscreen();
					}, 300)
					// #endif
				}
			},
			imLogin(uid, ims) {
				let getlogin = this.$tim.login({
					userID: uid,
					userSig: ims
				});
				getlogin.then(function(imResponse) {
					if (imResponse.data.repeatLogin === true) {
						console.log('imResponse.data.errorInfo', imResponse.data.errorInfo);
					} else {}
				}).catch(function(imError) {
					console.warn('重复登录', imError); // 登录失败的相关信息
				});
			},
			// 切换在线状态
			async ChangeOnlineStatus(type) {
				if(type == 'online') {
					console.log(111);
					await userApi.online()
				} else if(type == 'invisible') {
					console.log(333);
					await userApi.invisible()
				} else if(type == 'offline') {
					console.log(222);
					await userApi.offline()
				}
			}
		}
	}
</script>

<style>
	/* #ifndef APP-PLUS-NVUE */
	@import './theme/iconfont.css';
	@import './theme/index.css';

	.main {
		background-image: url('https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/app/yuebg.png');
		background-repeat: no-repeat;
		background-size: cover;
		background-position: 0 100%;
		min-height: 100vh;
	}

	.wxbutton {
		background-color: #70dec1;
		color: #FFFFFF;
		font-size: 28rpx;
		height: 90rpx;
		border-radius: 60rpx;
		width: 100%;
	}

	.wxbutton::after {
		border-width: 0;
	}

	button[disabled]:not([type]) {
		color: rgba(0, 0, 0, .3);
		background-color: #dedede
	}

	button[disabled]:not([type]) {
		color: #bfbfbf;
		background-color: #F2F2F2
	}

	/* #endif*/
</style>